package com.hrt.datasender.datacollect.drive.modbus.tcp;

import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.handler.codec.frame.FrameDecoder;

public class ModbusTCPFrameDecode extends FrameDecoder {
    
    private final int msgPrefixLength = 9;

    private int dataLength;

    public ModbusTCPFrameDecode(int dataLength) {
        this.dataLength = dataLength;
    }

    @Override
    protected Object decode(ChannelHandlerContext ctx, Channel channel, ChannelBuffer buffer) throws Exception {

        int expectDataLength = msgPrefixLength + dataLength * 2;
        byte[] decoded = new byte[expectDataLength];
        buffer.readBytes(decoded);

        return decoded;
    }

}
